package cn.nuist.controller;


import cn.nuist.entity.OrderItems;
import cn.nuist.entity.OrderSummary;
import cn.nuist.service.BillService;
import cn.nuist.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("bill")
public class BillController {
    private BillService billService;

    @Autowired
    public BillController(BillService billService) {
        this.billService = billService;
    }

    @RequestMapping("getAll")
    public String getAll(@RequestParam("target")String targetPageNum, Model model){
        //System.out.println("***" + targetPageNum);
        if (targetPageNum == null || targetPageNum.trim().length() == 0) {
            targetPageNum = "1";
        }
        Integer target = null;
        try {
            target = Integer.parseInt(targetPageNum);
        } catch (NumberFormatException e) {
            target = 1;
        }
        Page<OrderSummary> bills = billService.getViewPage(target);
        model.addAttribute("bills",bills);
        //System.out.println("***"+bills);
        return "bill/billList";
    }

    @RequestMapping("showdetail")
    public String getDetail(@RequestParam("orderID")Integer orderID,Model model){
        List<OrderItems> getdetail = billService.getdetail(orderID);
        model.addAttribute("bills",getdetail);
        return "bill/billDetail";
    }

    @RequestMapping("search")
    public String search(@RequestParam("target")String targetPageNum,String customerName,Model model){
        if (targetPageNum == null || targetPageNum.trim().length() == 0) {
            targetPageNum = "1";
        }
        Integer target = null;
        try {
            target = Integer.parseInt(targetPageNum);
        } catch (NumberFormatException e) {
            target = 1;
        }
        Page<OrderSummary> bills = billService.getBillByName(target, customerName);
        //System.out.println("****"+bills);
        model.addAttribute("bills",bills);
        return "bill/billList";
    }

    @RequestMapping("add")
    public String add(Model model){
        billService.add(model);
        return "bill/add";
    }

}
